/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pl.edu.amu.wmi.kino.visualjavafx.visualjavafxgui.editor.visual;

import java.awt.Component;
import java.awt.Point;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.io.IOException;
import org.netbeans.api.visual.widget.Scene;
import org.openide.util.Exceptions;
import pl.edu.amu.wmi.kino.visualjavafx.model.objects.VisualObject;
import pl.edu.amu.wmi.kino.visualjavafx.visualjavafxgui.editor.visual.VisualScene.WidgetCreator;
import pl.edu.amu.wmi.kino.visualjavafx.visualjavafxgui.editor.visual.palette.items.VisualObjectDrop;

/**
 *
 * @author psychollek
 */
class VisualJavaFxDropHandler extends DropTarget{

    public VisualJavaFxDropHandler(Component c, VisualScene scene) {
        super(c,new VJFxDropTargetListener(scene));
    }

}

class VJFxDropTargetListener implements DropTargetListener{

        Scene scene = null;

        VJFxDropTargetListener(VisualScene scene){
            this.scene = scene;
        }

        @Override
        public void dragEnter(DropTargetDragEvent dtde) {
            doDragOver(dtde);
        }

        @Override
        public void dragOver(DropTargetDragEvent dtde) {
            doDragOver(dtde);
        }

        @Override
        public void dropActionChanged(DropTargetDragEvent dtde) {
            //throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public void dragExit(DropTargetEvent dtde) {

        }

        @Override
        public void drop(DropTargetDropEvent dtde) {
            doDrop(dtde);
        }

        private void doDragOver(DropTargetDragEvent dtde) {
            dtde.acceptDrag( DnDConstants.ACTION_COPY_OR_MOVE );
        }

        private void doDrop(DropTargetDropEvent dtde) {
            VisualObjectDrop vod = retriveVisualObjectDrop(dtde);
            VisualObject vo = prepareVisualObject(vod, dtde);
            putToScene(vo);
            dtde.acceptDrop( DnDConstants.ACTION_COPY_OR_MOVE );
        }

        private VisualObject prepareVisualObject(VisualObjectDrop vod, DropTargetDropEvent dtde){
            VisualObject vo = vod.getVisualObject();
            Point loc = scene.convertViewToScene(dtde.getLocation());
            vo.setHPos(loc.getX());
            vo.setVPos(loc.getY());
            return vo;
        }

        private void putToScene( VisualObject vo){
            WidgetCreator wc = scene.getLookup().lookup(WidgetCreator.class);
            wc.addNew(vo);
        }

        // This is a litle hack, but will think how to do this better ... someday ...
        private VisualObjectDrop retriveVisualObjectDrop(DropTargetDropEvent dtde){
            for (int i = 0; i < dtde.getTransferable().getTransferDataFlavors().length; i++) {
                DataFlavor object = dtde.getTransferable().getTransferDataFlavors()[i];
                try {
                    if (object.isMimeTypeEqual("text/active_editor_flavor")){
                        if (dtde.getTransferable().getTransferData(object) instanceof VisualObjectDrop){
                            return (VisualObjectDrop) dtde.getTransferable().getTransferData(object);
                        }
                    }
                } catch (UnsupportedFlavorException ex) {
                    Exceptions.printStackTrace(ex);
                } catch (IOException ex) {
                    Exceptions.printStackTrace(ex);
                }
            }
            return null;
        }

    }
